<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PositionMonthTarget;
use App\Admin\Services\PositionMonthTargetService;
use App\Models\Brand;
use App\Models\Store;
use App\Models\UserMonthTarget;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PositionMonthTargetController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(PositionMonthTarget::with(['brand', 'store', 'department', 'position']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('target_date');
//            $grid->column('brand.title', admin_trans('position-month-target.fields.brand_id'));
            $grid->column('store.title', admin_trans('position-month-target.fields.store_id'));
            $grid->column('department.title', admin_trans('position-month-target.fields.department_id'));
            $grid->column('position.title', admin_trans('position-month-target.fields.position_id'));
            $grid->column('is_commission_coefficient_status', admin_trans('position-month-target.fields.is_coefficient_status'))->using(admin_trans('position-month-target.options.is_commission_coefficient_status'));
            $grid->column('meet_car_commission_coefficient', admin_trans('position-month-target.fields.coefficient'));
            $grid->column('output_value_commission_coefficient');
            $grid->column('gross_profit_reach');
            $grid->column('updated_at')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a href="/admin/user-month-target?position_month_target_id='.$actions->getKey().'"><i class="feather icon-eye"></i> 员工月目标</a>');
            });
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('store_id')
                    ->select(Store::all()->pluck('title', 'id'))
                    ->load('department_id', 'user-department');
                $filter->equal('department_id')->select()->load('position_id', 'user-position');
                $filter->equal('position_id')->select();
                $filter->equal('position_type')->select(admin_trans('position-month-target.options.position_type'));
                $filter->equal('target_date')->month();
            });

            $grid->disableBatchDelete();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PositionMonthTarget(), function (Show $show) {
            $show->field('id');
            $show->field('target_date');
            $show->field('brand_id');
            $show->field('store_id');
            $show->field('department_id');
            $show->field('position_id');
            $show->field('is_commission_coefficient_status');
            $show->field('meet_car_commission_coefficient');
            $show->field('output_value_commission_coefficient');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PositionMonthTarget(), function (Form $form) {
            $form->date('target_date')->format('YYYY-MM')->required();
            $form->select('brand_id')
                ->options(Brand::pluck('title', 'id'))
                ->load('store_id', 'user-store')
                ->required();
            $form->select('store_id')
                ->load('department_id', 'user-department')
                ->required();
            $form->select('department_id')
                ->load('position_id', 'user-position')
                ->required();
            $form->select('position_id')->required();

            $form->select('position_type')->options(admin_trans('position-month-target.options.position_type'))->when(1, function (Form $form) {
                $form->radio('is_commission_coefficient_status', admin_trans('position-month-target.fields.is_commission_coefficient_status'))
                    ->when([1], function (Form $form) {
                        $form->text('meet_car_commission_coefficient');
                        $form->text('output_value_commission_coefficient');
                    })
                    ->when(0, function (Form $form) {})
                    ->options(admin_trans('position-month-target.options.is_commission_coefficient_status'))
                    ->default(0);
            })->when(2, function (Form $form) {
                $form->radio('is_commission_coefficient_status', admin_trans('position-month-target.fields.is_append_coefficient_status'))
                    ->when([1], function (Form $form) {
                        $form->text('meet_car_commission_coefficient', admin_trans('position-month-target.fields.working_hours_coefficient'));
                        $form->text('output_value_commission_coefficient', admin_trans('position-month-target.fields.append_coefficient'));
//                        $form->text('working_hours_coefficient');
                })
                    ->when(0, function (Form $form) {})
                    ->options(admin_trans('position-month-target.options.is_commission_coefficient_status'))
                    ->default(0);
            });

            $form->text('gross_profit_reach');

            $form->saving(function ($form) {
                $param['id'] = $form->model()->id ?? 0;
                $param['target_date'] = $form->target_date;
                $param['store_id'] = $form->store_id;
                $param['department_id'] = $form->department_id;
                $param['position_id'] = $form->position_id;

                $is_target = PositionMonthTargetService::isTarget($param);

                if ($is_target) {
                    return $form->response()->error('该选择岗位所选择目标月份已经存在!');
                }
            });

            $form->footer(function ($footer) {
                $footer->disableReset();
                $footer->disableViewCheck();
                $footer->disableEditingCheck();
            });

            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });
        });
    }
}
